package com.lc.recovery.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.lc.recovery.R;
import com.lc.recovery.adapter.HomeAdapter;
import com.lc.recovery.app.AppFragment;
import com.lc.recovery.bean.ItemHomeBean;
import com.lc.recovery.http.request.BannerApi;
import com.lc.recovery.http.request.SceneApi;
import com.lc.recovery.http.request.ShopsTopApi;
import com.lc.recovery.http.request.StorageAdd;
import com.lc.recovery.http.request.WorkersCustomerApi;
import com.lc.recovery.http.response.BannerBean;
import com.lc.recovery.http.response.PublicMsg;
import com.lc.recovery.http.response.SceneBean;
import com.lc.recovery.http.response.ShopsTopBean;
import com.lc.recovery.http.response.WorkersCustomerBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.activity.HomeActivity;
import com.lc.recovery.ui.activity.LoginActivity;
import com.lc.recovery.ui.activity.VipCodeActivity;
import com.lc.recovery.ui.activity.recoveryhome.CustomerActivity;
import com.lc.recovery.ui.activity.recoveryhome.RecoveryShopActivity;
import com.lc.recovery.ui.adapter.ImageAdapter;
import com.lc.recovery.util.CourseUtils;
import com.lc.recovery.util.MyUtils;
import com.lc.recovery.util.SPUtil;
import com.lc.recovery.zxing.activity.MipcaActivityCapture;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class HomeRecoveryFragment extends AppFragment<HomeActivity> {



    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.rv_home_type)
    RecyclerView rvHomeType;
   private List<ItemHomeBean> mylist=new ArrayList<>();
    private String userId="1";
    private List<ShopsTopBean.DataBean> datas;
    private final int SCANNIN_GREQUEST_CODE = 107;//扫描二维码
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homerecovery;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        banner.setIndicator(new CircleIndicator(getContext()));
        banner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);

        shopsTop();
        setBannerData();


    }

    @Override
    public void onStart() {
        super.onStart();
        if (CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(), IntentKey.USERID,""))){
            workersCustomer();
        }else {
            tvWeight.setText("0");
            tvMoney.setText("0");
        }
    }

    private void workersCustomer() {
        EasyHttp.post(this)
                .api(new WorkersCustomerApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID,""))).request(new HttpCallback<WorkersCustomerBean>(this) {

            @Override
            public void onSucceed(WorkersCustomerBean data) {
                tvWeight.setText(data.getCustomers()+"");
                 tvMoney.setText(data.getToday()+"");
            }



            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void shopsTop() {

        EasyHttp.post(this)
                .api(new ShopsTopApi()).request(new HttpCallback<ShopsTopBean>(this) {

            @Override
            public void onSucceed(ShopsTopBean data) {
                for (int i=0;i<data.getData().size();i++){


                    if (i<2){
                        data.getData().get(i).setType(0);
                    }else {
                        data.getData().get(i).setType(1);
                    }

                }

                datas= data.getData();
                HomeAdapter adapter = new HomeAdapter(getContext(),data.getData(),userId);

                GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),6);


                rvHomeType.setLayoutManager(gridLayoutManager);
                rvHomeType.setAdapter(adapter);

            }



            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void setBannerData() {
        EasyHttp.post(this)
                .api(new BannerApi().setType("2")).request(new HttpCallback<BannerBean>(this) {

            @Override
            public void onSucceed(BannerBean data) {

                banner.setAdapter(new ImageAdapter(getContext(),data.getData())).setIndicator(new CircleIndicator(getContext())).setIndicatorGravity(IndicatorConfig.Direction.CENTER).setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(Object datas, int position) {

                    }
                });
            }



            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }
    private void scene(String scene) {

        EasyHttp.post(this)
                .api(new SceneApi().setScene(scene)).request(new HttpCallback<SceneBean>(this) {

            @Override
            public void onSucceed(SceneBean data) {



            }


            @Override
            public void onFail(Exception e) {
//              /  super.onFail(e);

            }
        });
    }
    //    RecoveryShopActivity
    @OnClick({R.id.icon_erweima, R.id.ll_recovery_shop, R.id.ll_customer, R.id.ll_recycle_today,R.id.rl_huiyuanma,R.id.rl_yijianruku})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.icon_erweima:
                XXPermissions.with(this)
                        .permission(Permission.CAMERA)
                        .request(new OnPermissionCallback() {

                            @Override
                            public void onGranted(List<String> permissions, boolean all) {
                                if (all) {
                                    Intent intent4 = new Intent();
                                    intent4.setClass(getActivity(), MipcaActivityCapture.class);

                                    startActivityForResult(intent4, SCANNIN_GREQUEST_CODE);
                                }else {

                                }
                            }
                        });

                break;

            case R.id.rl_huiyuanma:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(new Intent(getContext(), VipCodeActivity.class).putExtra("code_url",SPUtil.get(getContext(),IntentKey.URL,"")));
                break;
            case R.id.ll_recovery_shop:

                break;
//
            case R.id.ll_customer:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(), IntentKey.USERID, ""))) {
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(CustomerActivity.class);
                break;
            case R.id.ll_recycle_today:
                break;
            case R.id.rl_yijianruku:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(), IntentKey.USERID, ""))) {
                    startActivity(LoginActivity.class);
                    return;
                }
                storage_add();
                break;

        }
    }

    private void storage_add() {
        EasyHttp.post(this)
                .api(new StorageAdd().setWorker_id(SPUtil.get(getContext(),IntentKey.USERID,""))).request(new HttpCallback<PublicMsg>(this) {

            @Override
            public void onSucceed(PublicMsg data) {
                toast(data.getMsg());


            }

        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);  //这个super可不能落下，否则可能回调不了

        switch (SCANNIN_GREQUEST_CODE) {
            case SCANNIN_GREQUEST_CODE:
                if (resultCode == getActivity().RESULT_OK) {
                    String codeString = data.getStringExtra("result");
                    if (codeString.contains("user_id")) {
                        Log.e("TAG", "onActivityResult: "+codeString);
                        Log.e("TAG", "onActivityResult: "+ MyUtils.readValueFromUrlStrByParamName(codeString, "user_id") );
                        Log.e("TAG", "onActivityResult: "+ MyUtils.readValueFromUrlStrByParamName(codeString, "type" +
                                "") );
                        SPUtil.put(getContext(),IntentKey.BUSINESSUSERID,MyUtils.readValueFromUrlStrByParamName(codeString, "user_id") );
                        if (MyUtils.readValueFromUrlStrByParamName(codeString, "type").equals("2") )
                        {
                            toast("您无法回收该社区");
                            return;
                        }
                        if (datas!=null&&datas.size()>0){
                            startActivity(new Intent(getContext(), RecoveryShopActivity.class).putExtra("id",datas.get(0).getId()+"").putExtra("datas", (Serializable) datas).putExtra("type",MyUtils.readValueFromUrlStrByParamName(codeString, "type")));
//                     SPUtil.put(getContext(), IntentKey.BUSINESSUSERID,data.getUser_id());
                        }else {
                            toast("网络异常");
                        }
                        }

//


                }
                break;
        }
    }

}
